{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 10.3 实现全零填充\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设一张图像的大小为5×5×1，其像素矩阵如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "fda35add-808b-436a-a224-ea33268deb25",
   "metadata": {},
   "source": [
    "[[2,1,0,2,3],\n",
    "[9,5,4,2,0],\n",
    "[2,3,4,5,6],\n",
    "[1,2,3,1,0],\n",
    "[0,4,4,2,8]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61c78769-b47f-46fa-a721-5656f1cd3e3f",
   "metadata": {},
   "source": [
    "有一个3×3×1的卷积核，其像素矩阵如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d02a8fcd-1a2e-4833-9912-857ab407159c",
   "metadata": {},
   "outputs": [],
   "source": [
    "[[-1,0,1],\n",
    "[-1,0,1],\n",
    "[-1,0,1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3903b806-5cb1-4796-b785-eb466876dec8",
   "metadata": {},
   "source": [
    "要求：\n",
    "\n",
    "- 请使用卷积核对图像进行卷积计算，在卷积计算过程中请使用全零填充，并输出计算结果。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55043130-4496-43a3-803b-9bc1cea8b1b8",
   "metadata": {},
   "source": [
    "### 3.任务分析构\n",
    "\n",
    "只需将tf.nn.conv2d方法中的padding参数设置为'SAME'即可实现全零填充。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入形状: (1, 5, 5, 1)\n",
      "卷积核形状: (3, 3, 1, 1)\n",
      "输出形状: (1, 5, 5, 1)\n",
      "输出:\n",
      " [[ 7. -6. -1.  0. -3.]\n",
      " [10. -4.  1.  2. -8.]\n",
      " [11.  0. -1. -4. -7.]\n",
      " [10.  9.  0.  4. -7.]\n",
      " [ 7.  7. -2.  2. -2.]]\n"
     ]
    }
   ],
   "source": [
    "import  tensorflow as tf\n",
    "import numpy as np\n",
    "# 1，定义输入\n",
    "# 原始输入\n",
    "input=tf.constant([[2,1,0,2,3],\n",
    "                       [9,5,4,2,0],\n",
    "                       [2,3,4,5,6],\n",
    "                       [1,2,3,1,0],\n",
    "                       [0,4,4,2,8]],dtype=tf.float32)\n",
    "# 进行形状转换（增加批维度）\n",
    "input=tf.expand_dims(input,0)\n",
    "# 进行形状转换（增加通道维度）\n",
    "input=tf.expand_dims(input,3)\n",
    "print(\"输入形状:\",input.shape)\n",
    "\n",
    "# 2，定义卷积核\n",
    "# 维度格式：[filter_height, filter_width, in_channels, out_channels] \n",
    "filters=tf.constant([[-1,0,1],\n",
    "                         [-1,0,1],\n",
    "                         [-1,0,1]],dtype=tf.float32)\n",
    "# 进行形状转换（增加输入通道维度）\n",
    "filters=tf.expand_dims(filters,2)\n",
    "# 进行形状转换（增加输出通道维度）\n",
    "filters=tf.expand_dims(filters,3)\n",
    "print(\"卷积核形状:\",filters.shape)\n",
    "\n",
    "# 3，卷积计算\n",
    "out=tf.nn.conv2d(\n",
    "    input=input,   \n",
    "    filters=filters,\n",
    "    strides=1,\n",
    "    padding='SAME'\n",
    ")\n",
    "# 4，加偏置项\n",
    "b=tf.constant(1,dtype=tf.float32)\n",
    "out=out+b\n",
    "print(\"输出形状:\",out.shape)\n",
    "out=tf.squeeze(out)\n",
    "print(\"输出:\\n\",out.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
